package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dto.systemmanagement.rolepermission.RoleAllocationDTO;
import com.example.dto.systemmanagement.roles.RoleCreateDTO;
import com.example.entity.*;
import com.example.mapper.PermissionDataRuleMapper;
import com.example.mapper.PermissionMapper;
import com.example.mapper.RolePermissionMapper;
import com.example.mapper.RoleMapper;
import com.example.service.IRolePermissionService;
import com.example.utils.IdGenerate;
import com.example.utils.RedisCache;
import com.example.vo.systemmanagement.role.PermissionDisplayVO;
import com.example.vo.systemmanagement.role.RoleCreateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author diandian
 * @since 2024-04-17
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    PermissionDataRuleMapper permissionDataRuleMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    IdGenerate idGenerate;
    @Autowired
    RedisCache redisCache;
    Role role =new Role();
    RolePermission rolePermission=new RolePermission();



    @Override
    public RoleCreateVO definedRole(@RequestBody RoleCreateDTO roleCreateDTO,String userId) {
        role.setId(idGenerate.nextUUID(roleCreateDTO));
        role.setName(roleCreateDTO.getRoleName());
        role.setDescription(roleCreateDTO.getDescription());
        role.setCreateTime(LocalDateTime.now());
        role.setCreateBy(userId);
        roleMapper.insert(role);
        List<String> permissions=roleCreateDTO.getPermissions();
        for(String name:permissions){
            Permission p= permissionMapper
                    .selectOne(new LambdaQueryWrapper<Permission>().eq(Permission::getName,name));
            rolePermission.setId(idGenerate.nextUUID(role.getId()+p.getId()));
            rolePermission.setPermissionId(p.getId());
            rolePermission.setRoleId(role.getId());
            rolePermission.setCreateBy(userId);
            rolePermission.setCreateTime(LocalDateTime.now());
            rolePermissionMapper.insert(rolePermission);
        }

        RoleCreateVO roleCreateVO=new RoleCreateVO();
        roleCreateVO.setDescription(String.valueOf(roleMapper
                .selectOne(new LambdaQueryWrapper<Role>().eq(Role::getId, role.getId())).getDescription()));
        roleCreateVO.setRoleName(String.valueOf(roleMapper
                .selectOne(new LambdaQueryWrapper<Role>().eq(Role::getId, role.getId())).getName()));
//        roleCreateVO.setPermissionsName(role.getName());
        roleCreateVO.setPermissionsName(roleCreateDTO.getPermissions());
        return roleCreateVO;
    }

    @Override
    public PermissionDisplayVO permissionDisplay(String roleId) {
        List<Permission> permissions=permissionMapper.SearchListById(roleId);
        PermissionDisplayVO vo=new PermissionDisplayVO();
        List<String> permissionId=permissions.stream()
                .map(Permission::getId)
                .collect(Collectors.toList());
        vo.setPermission(permissionId);
        return vo;
    }

    @Override
    public boolean roleAllocation(RoleAllocationDTO allocationDTO,String userId) {
        try{
            for(String permission:allocationDTO.getAdd()){
                rolePermissionMapper.insert(new RolePermission(
                        idGenerate.nextUUID(allocationDTO),
                        allocationDTO.getRoleId(),
                        permission,
                        LocalDateTime.now(),
                        userId,
                        null,
                        null));
            }
            for(String permission:allocationDTO.getDel()){
                rolePermissionMapper.delete(new LambdaQueryWrapper<RolePermission>()
                        .eq(RolePermission::getPermissionId,permission)
                        .eq(RolePermission::getRoleId,allocationDTO.getRoleId()));
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
